import http from './http'

export default {
  login (data) {
    return http.post(`user/login?_format=json`, data).then(data => {
      const { csrf_token } = data
      http.setToken(csrf_token)
      // localStorage.setItem('logout_token', logout_token)
    })
  },

  // logout () {
  //   const token = localStorage.getItem('logout_token')
  //   return http.post(`user/logout?_format=json&token=${token}`).then(data => {
  //     localStorage.removeItem('logout_token')
  //   })
  // },
  logout () {
    return http.get(`user/logout`)
  },

  register (data) {
    // return http.post(`user/register?_format=json`, data)
    return http.post(`api/user`, data)
  },

  search (name) {
    return http.get(`api/user/${name}?_format=json&search=1`)
  },

  getLoginStatus () {
    return http.get(`user/login_status?_format=json`)
  },

  smsLogin ({ phone, code }) {
    return http.post(`api/sms_login?_format=json`, http.buildQuery({ phone, code }))
  },

  updatePassword ({ newPass, currentPass, context }) {
    const pack = {
      new_pass: newPass,
      current_pass: currentPass,
      context
    }
    return http.post(`api/update_password?_format=json`, http.buildQuery(pack))
  }
}
